package com.example.lt.controller;

import com.example.lt.constant.MessageConstant;
import com.example.lt.context.BaseContext;
import com.example.lt.pojo.entity.RankList;
import com.example.lt.result.Result;
import com.example.lt.service.RankListService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 小涛Tao
 * @Date: 2024/06/02/16:25
 * version 1.0 注释：
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/rankList")
public class RankListController {

    private final RankListService rankListService;

    /**
     * 根据歌单id查询歌单的评分
     * @param songListId
     * @return
     */
    @GetMapping("/common/id")
    public Result<Integer> getRankBySongListId(@RequestParam Long songListId) {
        log.info("查询歌单评分：{}",songListId);
        Integer rank = rankListService.getRankByListId(songListId);
        return Result.success(rank);
    }

    /**
     * 获取用户对歌单的评分，根据用户id和歌单id的评分
     * @return
     */
    @GetMapping("/user/andUser")
    public Result getUserRank(@RequestParam Long consumerId, @RequestParam Long songListId) {
        log.info("获取用户：{}对歌单：{}的评分",consumerId,songListId);
        Integer userRank = rankListService.getUserRank(consumerId, songListId);
        return Result.success(MessageConstant.SEEK_SUCCESS,userRank);
    }

    /**
     * 保存用户的评分
     * @param rankList
     * @return
     */
    @PostMapping("/user/add")
    public Result addRank(@RequestBody RankList rankList) {
        log.info("保存用户的评分：{}",rankList);
        // consumerId 改成ThreadLocal中的用户id
        rankList.setConsumerId(Long.valueOf(BaseContext.getCurrentId()));
        rankListService.addRank(rankList);
        return Result.success(MessageConstant.RANK_SUCCESS);
    }

    /**
     * 删除用户评分，重新评价
     * @param consumerId
     * @param songListId
     * @return
     */
    @DeleteMapping("/user/delete")
    public Result delete(@RequestParam Long consumerId, @RequestParam Long songListId) {
        consumerId = Long.valueOf(BaseContext.getCurrentId());
        log.info("删除用户评分，重新评价：{}，{}",consumerId,songListId);
        Integer flag = rankListService.delete(consumerId, songListId);
        if (flag > 0) {
            return Result.success(MessageConstant.RESET_RANK_SUCCESS);
        } else {
            return Result.error(MessageConstant.RESET_RANK_FAIL);
        }
    }

}
